package com.fs.uc.core.permcheck;

import com.fs.uc.base.domain.Permission;

import java.util.Collection;

/**
 * @author 崔超
 * 2021/12/9
 */
class PermissionCompare {

    static boolean comparePermissions(String action, String resource, Collection<Permission> permissions) {
        for (Permission p : permissions) {
            if (comparePermission(action, resource, p)) {
                return true;
            }
        }
        return false;
    }

    private static boolean comparePermission(String action, String resource, Permission permission) {
        return compareAction(action, permission.action) && compareResource(resource, permission.resource);
    }

    /*
    比对action（method）
    param： src-用户目标动作，dest-允许动作
     */
    private static boolean compareAction(String src, String dest) {
        if (dest.equals("*")) {
            return true;
        }
        return src.equals(dest);
    }

    /*
    比对resource（path）
    param： src-用户目标资源，dest-允许资源
     */
    private static boolean compareResource(String src, String dest) {
        return src.startsWith(dest);
    }
}
